//查询水泵信息
struct UID
{
	int type;
	int UID;
	int flow;
	int temperature;
	int liquidlevel;
	float stress;
	char valve[10];
};

//查看指定水泵信息
int ViewpumpInformation()
{
	int listen_sock = 0;
	int sock1 = 0,sock2 = 0;
	int ret = 0;
	
	//指向sockaddr_in的结构体指针，本地信息地址  绑定IP用的
	struct sockaddr_in addr;
	memset(&addr,0,sizeof(addr));
 	
 	//创建套接字
 	listen_sock = socket(AF_INET,SOCK_STREAM,0);
	if(listen_sock == -1)
	{
		perror("socket");
		exit(-1);
	}
 
 	addr.sin_family = AF_INET;//协议族
 	addr.sin_port = htons(6666);//端口
 	addr.sin_addr.s_addr = inet_addr("192.168.118.128");//IP
	
	ret = bind(listen_sock,(struct sockaddr *)&addr,sizeof(struct sockaddr));
	if(ret == -1)
	{
		perror("bind");
		exit(-1);
	}
	//监听
	ret = listen(listen_sock,5);
	if(ret == -1)
	{
		perror("listen");
	}
	
	struct epoll_event ev, events[100];
        int  conn_sock, nfds, epollfd;

	epollfd = epoll_create1(0);//创建一个epoll句柄
        if (epollfd == -1)
        {
     		perror("epoll_create1");
         	exit(EXIT_FAILURE);
        }

	ev.events = EPOLLIN;//表示相应的文件描述符能够读
    	ev.data.fd = listen_sock;//设置与要处理事件相关的文件描述符
	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1) 
	{
		perror("epoll_ctl: listen_sock");
		exit(EXIT_FAILURE);
	}
	for (;;) 
	{
		 //等待事件的产生，返回需要处理的事件的数量，并将需处理时间的套接字集合于events中，遍历events处理事件
		nfds = epoll_wait(epollfd, events, 100, -1); //nfds:活跃连接个数
		if (nfds == -1)
		{
			 perror("epoll_wait");
			 exit(EXIT_FAILURE);
		}
		int n = 0;
		for (n = 0; n < nfds; n++)
		{
			if (events[n].data.fd == listen_sock)
			{
				conn_sock = accept(listen_sock,NULL,NULL);
				if (conn_sock == -1)
				{
					perror("accept");
					exit(EXIT_FAILURE);
				}
				ev.events = EPOLLIN | EPOLLET;
				ev.data.fd = conn_sock;
				if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock,&ev) == -1) 
				{
					perror("epoll_ctl: conn_sock");
					exit(EXIT_FAILURE);
				}
			} 
			else 
			{
				int ret = 0;
				char buf[1024] = {0};
				int type = 0;
				int num = 0;	

				ret = recv(events[n].data.fd,buf,sizeof(buf),0);
				if(ret == -1)
				{
				perror("recv");
				exit(-1);
				}
				printf("%s\n",buf);
				
				info.type = type;

				//定义cJSON指针
				cJSON *json = NULL;
				//将字符串转为json格式对象
				json = cJSON_Parse(buf);

				cJSON *node = NULL;
				//由键获取值
				node = cJSON_GetObjectItem(json,"type");
				if(node == NULL)
				{
					printf("can't find type\n");
					exit(-1);
				}
				if(node->type == cJSON_Number)
				{
					type = node->valueint;
				}
				node = cJSON_GetObjectItem(json,"num");
				if(node == NULL)
				{
					printf("can't find num\n");
					exit(-1);
				}
				if(node->type == cJSON_Number)
				{
					num = node->valueint;
				}
				
				cJSON_Delete(json);	
			}
		}
	}
	return num;
}

//查看指定水泵信息
struct gather* Viewinformation(int num)
 {
    	num = ViewpumpInformation();
    	
    	MYSQL *mysql = NULL;
  	MYSQL *rs = NULL;
  	
  	if(num == 1)
  	{
		char sql[100] = {"select * from wph1"}; 	
  	}
  	if(num == 2)
  	{
		char sql[100] = {"select * from wph2"}; 	
  	}
  	if(num == 3)
  	{
		char sql[100] = {"select * from wph3"}; 	
  	}

	mysql = mysql_init(NULL);

	rs = mysql_real_connect(mysql,"localhost","","","water",0,NULL,0);
	if(rs == NULL)
	{
		printf("%s\n",mysql_error(mysql));
		mysql_close(mysql);
		exit(-1);
 	}
 	
 	struct gather info;
	memset(&info,0,sizeof(info));
 	
 	res = mysql_use_result(mysql);
	line = mysql_water_fields(res);//读取结果集的列数
 	
 	if(info.type == 6)
	{
		
		MYSQL_RES *res = NULL;
		MYSQL_ROW row = NULL;

		    res = mysql_store_result(mysql);
		    
		        struct UID node;
			memset(&node,0,sizeof(node));

		        node->type = atoi(row[0]);
		        node->UID = atoi(row[1]);
		        node->flow = atoi(row[2]);
		        node->temperature = atof(row[3]);
		        node->liquidlevel = atof(row[4]);
		        node->stress = atof(row[5]);
		        strcpy(node->valve,row[6]);

		        node->next = NULL;
		        cur->next = node;
		        cur = cur->next;
		        row = mysql_fetch_row(res);
		    }

		    mysql_free_result(res);
		    mysql_close(mysql);
	}

 	int ret = 0;
 	ret = mysql_query(mysql,sql);
 	if(ret != 0)
 	{
 		printf("%s\n",mysql_error(mysql));
 		mysql_close(mysql);
 		exit(-1);
 	}

 	int ret1 = 0;
	ret1 = mysql_real_query(mysql,sql,strlen(sql));//关闭
	if(ret1 != 0)
	{
		printf("%s\n",mysql_error(mysql));
		mysql_close(mysql);
		exit(-1);
	}
	mysql_close(mysql);
	
	//定义cJSON 指针并创建json对象
	cJSON *json = NULL;
	json = cJSON_CreateObject();
	
	//添加多个"键-值”对
	cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(type));
	cJSON_AddItemToObject(json,"UID",cJSON_CreateNumber(da.UID));
	cJSON_AddItemToObject(json,"flow",cJSON_CreateNumber(da.flow));
	cJSON_AddItemToObject(json,"temperature",cJSON_CreateNumber(da.temperature));
	cJSON_AddItemToObject(json,"liquidlevel",cJSON_CreateNumber(da.liquidlevel));
	cJSON_AddItemToObject(json,"stress",cJSON_CreateNumber(da.stress));
	cJSON_AddItemToObject(json,"valve",cJSON_CreateString(da.valve));
	
	//将json对象转成json格式字符串
	char *p = cJSON_Print(json);
		
	//调申请连接函数
	int sock = 0;
	int ret = 0;	
	sock = requset_connect();
	
	//发送
	ret = send(sock,p,strlen(p),0);
	if(ret == -1)
	{
		perror("send");
		exit(-1);
	
	}
}
